﻿using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using XUpdater;

namespace XUtliPoolLib
{
	// Token: 0x020001CD RID: 461
	public sealed class XTableAsyncLoader
	{
		// Token: 0x170000C3 RID: 195
		// (get) Token: 0x06000A95 RID: 2709 RVA: 0x00038A8C File Offset: 0x00036C8C
		public bool IsDone
		{
			get
			{
				bool ret = false;
				if (this.currentXfra == null)
				{
					if (XTableAsyncLoader.currentThreadCount < XTableAsyncLoader.AsyncPerTime)
					{
						XTableAsyncLoader.currentThreadCount++;
					}
					ret = this.InnerExecute();
				}
				else
				{
					if (this.currentXfra.IsDone)
					{
						if (XTableAsyncLoader.currentThreadCount < XTableAsyncLoader.AsyncPerTime)
						{
							XTableAsyncLoader.currentThreadCount++;
						}
						XBinaryReader.Return(this.currentXfra.Data, false);
						this.currentXfra = null;
						ret = this.InnerExecute();
					}
				}
				if (ret)
				{
					if (this._call_back != null)
					{
						this._call_back();
						this._call_back = null;
					}
					this._executing = false;
				}
				return ret;
			}
		}

		// Token: 0x06000A96 RID: 2710 RVA: 0x00038B60 File Offset: 0x00036D60
		public void AddTask(string location, CVSReader reader, bool native = false)
		{
			INativePlugin nativePlugin = XSingleton<XUpdater.XUpdater>.singleton.XPlatform.GetNativePlugin();
			if (native && nativePlugin != null)
			{
				TextAsset sharedResource = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<TextAsset>(location, ".bytes", true, false);
				if (sharedResource != null)
				{
					nativePlugin.InputData(0, 0, sharedResource.bytes, sharedResource.bytes.Length);
					XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(location, ".bytes", sharedResource, false);
				}
			}
			else
			{
				XFileReadAsync xfileReadAsync = new XFileReadAsync();
				xfileReadAsync.Location = location;
				xfileReadAsync.Reader = reader;
				this._task_list.Add(xfileReadAsync);
			}
		}

		// Token: 0x06000A97 RID: 2711 RVA: 0x00038C04 File Offset: 0x00036E04
		private bool InnerExecute()
		{
			bool result;
			if (this._task_list.Count > 0)
			{
				if (XTableAsyncLoader.currentThreadCount <= 0)
				{
					result = false;
				}
				else
				{
					XTableAsyncLoader.currentThreadCount--;
					this.currentXfra = this._task_list[this._task_list.Count - 1];
					this._task_list.RemoveAt(this._task_list.Count - 1);
					this.ReadFileAsync(this.currentXfra);
					result = false;
				}
			}
			else
			{
				result = true;
			}
			return result;
		}

		// Token: 0x06000A98 RID: 2712 RVA: 0x00038C94 File Offset: 0x00036E94
		public bool Execute(OnLoadedCallback callback = null)
		{
			bool result;
			if (this._executing)
			{
				result = false;
			}
			else
			{
				this._call_back = callback;
				this._executing = true;
				this.InnerExecute();
				result = true;
			}
			return result;
		}

		// Token: 0x06000A99 RID: 2713 RVA: 0x00038CCC File Offset: 0x00036ECC
		private void ReadFileAsync(XFileReadAsync xfra)
		{
			xfra.Data = XBinaryReader.Get();
			XSingleton<XDebug>.singleton.AddWarningLog("zcg ReadFileAsync....Location:"+ xfra.Location);
			if (!XSingleton<XResourceLoaderMgr>.singleton.ReadText(xfra.Location, ".bytes", xfra.Data, false))
			{
				XResourceLoaderMgr.LoadErrorLog(xfra.Location);
				XBinaryReader.Return(xfra.Data, false);
				xfra.Data = null;
				xfra.IsDone = true;
				this.currentXfra = null;
			}
			else
			{
     //           if (xfra.Location == "Table/TaskListNew")
     //           {
     //               XSingleton<XDebug>.singleton.AddWarningLog("zcg TaskListNew..2222");
     //               try
     //               {
     //                   if (!xfra.Reader.ReadFile(xfra.Data))
     //                   {
     //                       XSingleton<XDebug>.singleton.AddErrorLog("in File: ", xfra.Location, xfra.Reader.error, null, null, null);
     //                   }
     //               }
     //               catch (Exception ex)
     //               {
     //                   XSingleton<XDebug>.singleton.AddErrorLog(ex.Message, " in File: ", xfra.Location, xfra.Reader.error, null, null);
     //               }
     //               xfra.IsDone = true;
					//return;
     //           }
                ThreadPool.QueueUserWorkItem(delegate(object state)
				{
					try
					{
						if (!xfra.Reader.ReadFile(xfra.Data))
						{
							XSingleton<XDebug>.singleton.AddErrorLog("in File: ", xfra.Location, xfra.Reader.error, null, null, null);
						}
					}
					catch (Exception ex)
					{
						XSingleton<XDebug>.singleton.AddErrorLog(ex.Message, " in File: ", xfra.Location, xfra.Reader.error, null, null);
					}
					xfra.IsDone = true;
				});
			}
		}

		// Token: 0x04000504 RID: 1284
		private bool _executing = false;

		// Token: 0x04000505 RID: 1285
		private List<XFileReadAsync> _task_list = new List<XFileReadAsync>();

		// Token: 0x04000506 RID: 1286
		private OnLoadedCallback _call_back = null;

		// Token: 0x04000507 RID: 1287
		private XFileReadAsync currentXfra = null;

		// Token: 0x04000508 RID: 1288
		public static int currentThreadCount = 0;

		// Token: 0x04000509 RID: 1289
		public static readonly int AsyncPerTime = 2;
	}
}
